<template>
  <div class="article-page">
    <nav class="my-nav van-hairline--bottom">
      <a
        @click="changeSorter('weight_desc')"
        href="javascript:;"
        :class="{ active: sorter === 'weight_desc' }"
        >推荐</a>
      <a
        @click="changeSorter(null)"
        href="javascript:;"
        :class="{ active: sorter === null }"
        >最新</a>
      <div class="logo"><img src="@/assets/logo.png" alt=""></div>
    </nav>
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <ArticleItem v-for="item in list" :key="item.id" :item="item"></ArticleItem>
    </van-list>
  </div>
</template>

<script>
import { getArticles } from '@/api/article'
export default {
  name: 'article-page',
  data () {
    return {
      list: [],
      current: 1, // 当前在第几页
      loading: false, // 是否正在加载
      finished: false, // 是否全部加载完成, 如果设置为 true 则以后再也不会触发 load 事件了
      sorter: 'weight_desc'
    }
  },
  // async created () {
  //   const res = await getArticles({
  //     current: this.current,
  //     pageSize: 10
  //   })
  //   // console.log(res.data.rows)
  //   this.list = res.data.rows
  // },
  methods: {
    changeSorter (sorter) {
      // 修改 sorter
      this.sorter = sorter
      // 重置所有数据
      this.list = []
      this.current = 1
      this.finished = false
      // 重新发请求
      this.onLoad()
      // 已经手动发请求了, 就不要自动触发了
      this.loading = true // 表示已经在加载了
    },
    // 当页面滚动到底部时会触发 load 事件, 并将 loading 改为 true
    // 此时我们可以发起请求获取下一页的数据
    // 获取到数据后将数据合并到当前数组中
    // 然后将 loading 设置为 false
    // 记得让当前页 ++
    async onLoad () {
      // console.log('触发事件了, 要加载更多啦!')
      const res = await getArticles({
        current: this.current,
        pageSize: 10,
        sorter: this.sorter
      })
      // 得到结果马上判断是否在最后一页
      if (this.current >= res.data.pageTotal) {
        // 到最后一页了
        this.finished = true
      }
      // console.log(res.data.rows)
      // this.list = res.data.rows
      // 把下一页数据合并到当前数据中
      this.list.push(...res.data.rows)
      // 加载完毕后将 loading 设为 false
      this.loading = false
      // 翻一页
      this.current++
    }
  }
}
</script>

<style lang="less" scoped>
.article-page {
  margin-bottom: 50px;
  margin-top: 44px;
  .my-nav {
    height: 44px;
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    z-index: 999;
    background: #fff;
    display: flex;
    align-items: center;
    > a {
      color: #999;
      font-size: 14px;
      line-height: 44px;
      margin-left: 20px;
      position: relative;
      transition: all 0.3s;
      &::after {
        content: '';
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
        bottom: 0;
        width: 0;
        height: 2px;
        background: #222;
        transition: all 0.3s;
      }
      &.active {
        color: #222;
        &::after {
          width: 14px;
        }
      }
    }
    .logo {
      flex: 1;
      display: flex;
      justify-content: flex-end;
      > img {
        width: 64px;
        height: 28px;
        display: block;
        margin-right: 10px;
      }
    }
  }
}
</style>
